﻿using System.Collections.Generic;
using System.Linq;
using HCQ2_Model;
using HCQ2_Common.Bean;
using HCQ2_Common.Constant;
using HCQ2_Model.Constant;

namespace HCQ2UI_Helper.Session
{
    /// <summary>
    ///  权限验证缓存
    /// </summary>
    public class SysPermissSession
    {
        /// <summary>
        ///  获取系统选择语言
        /// </summary>
        public static string sysLanguage
        {
            get
            {
                var langHeader = string.Empty;
                //1.0 获取cookie中保存的语言设置
                langHeader = HCQ2_Common.RequestHelper.GetCookie("Language");
                //2.0 获取字典默认语言
                if (string.IsNullOrEmpty(langHeader))
                    langHeader = OperateContext.Current.bllSession.T_ItemCode.FindSingle(s => s.item_code.Equals("SysDefaultLanuage"))?.item_note;
                //3.0 都为空时设置默认语言为中文
                if (string.IsNullOrEmpty(langHeader))
                    langHeader = "cn";

                //if (OperateContext.Current.Usr != null)
                //{
                //    #region 已登录
                //    string tempLang = OperateContext.Current.Usr.lang;
                //    if (string.IsNullOrEmpty(tempLang))
                //    {
                //        tempLang= OperateContext.Current.bllSession.T_ItemCode.FindSingle(s => s.item_code.Equals("SysDefaultLanuage"))?.item_note;
                //        if(string.IsNullOrEmpty(tempLang))
                //            langHeader = "cn";
                //    }
                //    else
                //    {
                //        if (tempLang.Equals("cn-CN"))
                //            langHeader = "cn";
                //        else if (tempLang.Equals("en-US"))
                //            langHeader = "en";
                //        else
                //            langHeader = tempLang;
                //    }
                //    #endregion
                //}
                //else
                //{
                //    #region 未登录
                //    //1.0 获取cookie中保存的语言设置
                //    langHeader = HCQ2_Common.RequestHelper.GetCookie("Language");
                //    //2.0 获取字典默认语言
                //    if (string.IsNullOrEmpty(langHeader))
                //        langHeader = OperateContext.Current.bllSession.T_ItemCode.FindSingle(s => s.item_code.Equals("SysDefaultLanuage"))?.item_note;
                //    //3.0 都为空时设置默认语言为中文
                //    if (string.IsNullOrEmpty(langHeader))
                //        langHeader = "cn";
                //    #endregion
                //}
                return langHeader;
            }
        }
        /// <summary>
        ///  是否为中文语言
        /// </summary>
        public static bool isChinese
        {
            get
            {
                if (sysLanguage.Equals("cn"))
                    return true;
                return false;
            }
        }
        /// <summary>
        ///  获取附件服务器集合
        /// </summary>
        public static List<T_ItemCodeMenum> serviceList
        {
            get
            {
                object cache = SessionHelper.GetSessionValue(CacheConstant.serviceData);
                List<T_ItemCodeMenum> list = (cache != null) ? (List<T_ItemCodeMenum>)cache : null;
                if (null == list)
                {
                    T_ItemCode code = OperateContext.Current.bllSession.T_ItemCode.FindSingle(s => s.item_code.Equals("docFileService"));
                    if (null != code)
                        list = OperateContext.Current.bllSession.T_ItemCodeMenum.Select(s => s.item_id == code.item_id);
                    SessionHelper.AddSessionValue(CacheConstant.serviceData, list);
                }
                return list;
            }
        }
        /// <summary>
        ///  获取附件服务器前缀
        /// </summary>
        /// <param name="service_id"></param>
        /// <returns></returns>
        public static string GetServiceUrl(string service_id)
        {
            if (string.IsNullOrEmpty(service_id))
                return string.Empty;
            List<T_ItemCodeMenum> list = serviceList;
            if (null == serviceList || serviceList.Count <= 0)
                return string.Empty;
            return list.Where(s => s.code_name.Equals(service_id)).FirstOrDefault()?.code_value;
        }
        /// <summary>
        ///  获取当前登录用户 角色集合
        /// </summary>
        public static List<int> RolesList
        {
            get
            {
                object cache = SessionHelper.GetSessionValue(CacheConstant.loginUserCacheRoles);
                List<int> list = (cache != null) ? (List<int>)cache : null;
                if (null == list)
                {
                    list= OperateContext.Current.bllSession.T_Permissions.GetRolesById(OperateContext.Current.Usr.user_id);
                    SessionHelper.AddSessionValue(CacheConstant.loginUserCacheRoles,list);
                }
                return list;
            }
        }
        /// <summary>
        ///  获取当前登录用户 权限集合
        /// </summary>
        public static List<T_Permissions> PermissList
        {
            get
            {
                object cache = SessionHelper.GetSessionValue(CacheConstant.loginUserCachePermiss);
                List<T_Permissions> list = (cache != null) ? (List<T_Permissions>)cache : null;
                if (null == list)
                {
                    list= OperateContext.Current.bllSession.T_Permissions.GetPermissById(OperateContext.Current.Usr.user_id);
                    SessionHelper.AddSessionValue(CacheConstant.loginUserCachePermiss,list);
                }
                return list;
            }
        }
        /// <summary>
        ///  获取当前登录用户 菜单集合
        /// </summary>
        public static List<T_PageFolder> MenusList
        {
            get
            {
                return OperateContext.Current.bllSession.T_Permissions.GetMenusById(OperateContext.Current.Usr.user_id); 
            }
        }
        /// <summary>
        ///  获取当前登录用户 所有元素集合
        /// </summary>
        public static List<T_PageElement> ElementsList
        {
            get
            {
                object cache = SessionHelper.GetSessionValue(CacheConstant.allCacheElements);
                List<T_PageElement> list = (null != cache) ? (List<T_PageElement>)cache : null;
                if (null == list)
                {
                    list= OperateContext.Current.bllSession.T_Permissions.GetElementsById(OperateContext.Current.Usr.user_id);
                    SessionHelper.AddSessionValue(CacheConstant.allCacheElements,list);
                }
                return list;
            }
        }
        /// <summary>
        ///  判断是否有权访问页面
        /// </summary>
        /// <param name="controller">控制器</param>
        /// <param name="action">方法名</param>
        /// <returns></returns>
        public static bool CheckPermiss(string controller, string action)
        {
            if (null == MenusList)
                return false;
            var query = MenusList.Where(s => s.folder_url == (controller + "/" + action)).ToList();
            if (query.Count <= 0)
                return false;
            return true;
        }
        /// <summary>
        ///  获取请求页面的元素集合
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static List<T_PageElement> GetElementByUser(string controller,string action)
        {
            if (null == MenusList)
                return null;
            T_PageFolder folder =
                MenusList.FirstOrDefault(s => s.folder_url == (controller + "/" + action));
            if (null == folder)
                return null;
            if (null == ElementsList)
                return null;
            return ElementsList.Where(s => s.folder_id == folder.folder_id).ToList();
        }
        /// <summary>
        ///  获取当前登录用户 指定页面的元素id集合
        /// </summary>
        /// <param name="controller">controller</param>
        /// <param name="action">action</param>
        /// <param name="pageType">页面类型参数</param>
        /// <returns></returns>
        public static List<string> GetElementCodeByUser(string controller, string action,string pageType=null,string vrType=null,string approveType=null)
        {
            List<string> list = new List<string>();
            if (null == MenusList)
                return list;
            T_PageFolder folder = null;
            if (!string.IsNullOrEmpty(pageType))
            {
                var _folder = MenusList.Where(s => (!string.IsNullOrEmpty(s.folder_url)) && s.folder_url.Contains(controller + "/" + action) && s.folder_url.Contains("pageType=" + pageType));
                if (!string.IsNullOrEmpty(vrType))
                    _folder = _folder.Where(s => s.folder_url.Contains($"vrType={vrType}"));
                if (!string.IsNullOrEmpty(approveType))
                    _folder = _folder.Where(s => s.folder_url.Contains($"operator={approveType}"));
                folder = _folder?.FirstOrDefault();
            }
            else
                folder = MenusList.FirstOrDefault(s => (!string.IsNullOrEmpty(s.folder_url)) && s.folder_url.Contains(controller + "/" + action));
            if (null == folder)
                return list;
            if (null == ElementsList)
                return list;
            return ElementsList.Where(s => s.folder_id == folder.folder_id).Select(s => s.pe_code).ToList();
        }
        
        /// <summary>
        ///  获取当前登录用户分配 模块集合
        /// </summary>
        public static List<T_SysModule> SysModule
        {
            get
            {
                return OperateContext.Current.bllSession.T_SysModule.GetPermissById(OperateContext.Current.Usr.user_id);
            }
        }
        /// <summary>
        ///  判断当前登录用户是否属于 区域管理员
        /// </summary>
        public static bool isAreaManager
        {
            get
            {
                if (PermissList == null || PermissList.Count <= 0)
                    return false;
                var query = PermissList.Where(s => s.per_type == "areaAdmin");
                return (query != null && query.Count() > 0) ? true : false;
            }
        }
        /// <summary>
        ///  将用户传递的guid转换为user_id
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static int ChangeIdByGuid(string guid)
        {
            if (string.IsNullOrEmpty(guid))
                return 0;
            T_User user = OperateContext.Current.bllSession.T_User.Select(s => s.user_guid == guid).FirstOrDefault();
            return user != null ? user.user_id : 0;
        }
        /// <summary>
        ///  将用户传递的user_id转换为guid
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string ChangeGuidById(int id)
        {
            if (id <= 0)
                return string.Empty;
            var u = OperateContext.Current.bllSession.T_User.FindSingle(s => s.user_id == id);
            return u != null ? u.user_guid : string.Empty;
        }
        /// <summary>
        ///  获取授权限定提示信息
        /// </summary>
        /// <returns></returns>
        public static string GetPowerMessage
        {
            get
            {
                string temp= OperateContext.Current.bllSession.T_ItemCode.FindSingle(s => s.item_code.Equals("docPowerMessage"))?.item_note;
                if (string.IsNullOrEmpty(temp))
                    temp = "当前用户授权被限制，请联系管理员处理！";
                return temp;
            }
        }
    }
}